package customerEnter.Frame.Thread;

import static com.google.common.collect.Maps.newHashMap;

import java.lang.reflect.Method;
import java.util.Collection;
import java.util.Map;

import customerEnter.tool.WhetherToEmpty;

public class ThreadFrame extends Thread {

	static Map<String, ClassNameAnntations>	methodAnnotation	= newHashMap();

	public static String getMethodName(String className) {
		Collection<StackTraceElement[]> stackTraceElements = getAllStackTraces().values();
		String name = "";
		for (StackTraceElement[] sts : stackTraceElements) {
			for (StackTraceElement stackTraceElement : sts) {
				if (className.equals(stackTraceElement.getClassName())) {
					name = stackTraceElement.getMethodName();
					break;
				}
			}
		}
		return name;
	}

	public static Method getMethodAnntations(Class<?> className, String methodName) {
		ClassNameAnntations classNameAnntations = methodAnnotation.get(className.getName());
		// 判斷有沒有保存這個類
		if (WhetherToEmpty.ObjectWhetherToEmpty(classNameAnntations)) {
			// 保存所有的方法註解
			methodAnnotation.put(className.getName(), addAnntation(className));
			// 獲得存在方法上的註解
			return methodAnnotation.get(className.getName()).getMethodAnnotation().get(methodName);
		} else {
			// 判斷整個類裡面有沒有註解
			if (classNameAnntations.getMethodAnnotation().size() > 0) {
				return classNameAnntations.getMethodAnnotation().get(methodName);
			}
		}
		return null;
	}

	private static ClassNameAnntations addAnntation(Class<?> className) {
		ClassNameAnntations classNameAnntations = new ClassNameAnntations();
		Method[] methods = className.getDeclaredMethods();
		for (Method method : methods) {
			if (method.getAnnotations().length > 0) {
				classNameAnntations.getMethodAnnotation().put(method.getName(), method);
			}
		}
		return classNameAnntations;
	}

}
